package com.woniuxy.opencv.service.impl;

import cn.hutool.core.util.RandomUtil;

import com.woniuxy.opencv.config.MqttConfig;
import com.woniuxy.opencv.pojo.ResData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Service
public class LockService {

    @Autowired
    private MqttConfig mqttConfig;



    public ResData handleLockOperation( Integer lockId) {
        try {
            String randomId = RandomUtil.randomString(8);
            String jsonMessage = String.format(
                    "{\"id\":\"%s\",\"lockId\":%d}", randomId, lockId
            );

            mqttConfig.publishMessage("community/access_control", jsonMessage);

            // 成功响应
            HashMap<String, Object> response = new HashMap<>();
            response.put("lockId", lockId);
             response.put("commandId", randomId);
            return new ResData(200, "指令发送成功", response);
        } catch (Exception e) {
            // 异常响应
            return new ResData(500, "服务异常：" + e.getMessage(), null);
        }
    }
}